home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / develop™ Technical Journal / develop Issue 23 code / Multipane Dialogs Code.sea / Multipane Dialogs Code / Sample.c / Sample.c
Encoding:
C/C++ Source or Header  |  1995-10-15  |  6.6 KB  |  296 lines  |  [TEXT/MMCC]

  1. #include "Sample.h"
  2. #include "MPDialogs.h"
  3. #include "DD.h"
  4. #include "SampleProcs.h"
  5.  
  6. /* Sample application demonstrating the use of the multi-pane dialog code
  7.  * Largly borrowed from the Sample Drag application in the
  8.  *   Macintosh Drag and Drop package.
  9.  */
  10.  
  11. // Comment out to use default Action Procedures.
  12. #define CUSTOM_PROCS
  13.  
  14. // Define this to use a modal dialog
  15. #define MODAL
  16.  
  17. // Menu constants
  18. #define kAppleMenuID    128
  19. #define kAboutMenuItem    1
  20. #define kFileMenuID        129
  21. #define kPrefsItem        1
  22. #define kAllocateHandle    2
  23. #define kDisplayItem    3
  24. #define kCloseItem        5
  25. #define kQuitMenuItem    7
  26. #define kAboutDLOG        128
  27. #define kPrefDLOG        200
  28.  
  29. // Global variables
  30. Boolean            gQuitFlag;        // true when the app should exit the main event loop
  31. DialogPtr        gAbout = NULL;    // modeless About dialog box
  32.  
  33. MenuHandle        gAppleMenuHandle, gFileMenuHandle; // For our menus
  34. DialogPtr        prefDlog = NULL; // The multi-pane dialog
  35. Handle            thePrefs = NULL; // Storage of the data entered in the dialog
  36.  
  37. // UPPs for the custom Action Procedures
  38. #ifdef CUSTOM_PROCS
  39. ClickActionUPP    myClickAction = NULL;
  40. EditActionUPP    myEditAction = NULL;
  41. #endif
  42.  
  43. /***************************************************
  44.  *  Typical Mac toolbox routines
  45.  ***************************************************/
  46.  
  47. // CreateMenus makes menus the old-fashioned way
  48. void CreateMenus()
  49. {
  50.     // create Apple menu
  51.     gAppleMenuHandle = GetMenu(kAppleMenuID);
  52.     AddResMenu(gAppleMenuHandle, 'DRVR');
  53.     InsertMenu(gAppleMenuHandle, 0);
  54.  
  55.     // create File menu
  56.     gFileMenuHandle = GetMenu(kFileMenuID);
  57.     InsertMenu(gFileMenuHandle, 0);
  58.     
  59.     DrawMenuBar();
  60. }
  61.  
  62. // DoMenuCommand handles user menu selections
  63. void DoMenuCommand(long menuVal)
  64. {
  65.     short theItem, theMenu;
  66.     Str255 deskAccessoryName;
  67.     WindowPtr whichWindow;
  68.     
  69.     theItem = LoWord(menuVal);
  70.     theMenu = HiWord(menuVal);
  71.  
  72.     switch (theMenu) {
  73.  
  74.         case kAppleMenuID:
  75.             if (theItem == kAboutMenuItem) {
  76.                 if (gAbout) SelectWindow(gAbout);
  77.                 else gAbout = GetNewDialog(kAboutDLOG, NULL, (WindowPtr) -1);
  78.             } else {
  79.                 GetItem(gAppleMenuHandle, theItem, deskAccessoryName);
  80.                 (void) OpenDeskAcc(deskAccessoryName);
  81.             }
  82.             break;
  83.  
  84.         case kFileMenuID:
  85.  
  86.             switch (theItem) {
  87.                 case kPrefsItem:
  88.                     if (!prefDlog) {
  89.                         prefDlog = OpenMPDialog(kPrefDLOG, NULL,
  90. #ifdef CUSTOM_PROCS
  91.                             myClickAction, myEditAction, NULL, &thePrefs);
  92. #else
  93.                             NULL, NULL, NULL, &thePrefs);
  94. #endif
  95.                     }
  96.                     if (prefDlog) SetMenusBusy();
  97.                     break;
  98.                 case kAllocateHandle:
  99.                     thePrefs = GetMPDialogHandle(kPrefDLOG,
  100. #ifdef CUSTOM_PROCS
  101.                                 myEditAction, NULL);
  102. #else
  103.                                 NULL, NULL);
  104. #endif
  105.                     break;
  106.                 case kDisplayItem:
  107.                     DialogDisplay(thePrefs);
  108.                     break;
  109.                 case kCloseItem:
  110.                     if (!(whichWindow = FrontWindow())) break;
  111.                     if (whichWindow == gAbout) {
  112.                         DisposeDialog(gAbout);
  113.                         gAbout = NULL;
  114.                     } else if (whichWindow == prefDlog) {
  115.                         CloseMPDialog(&prefDlog);
  116.                         if (!prefDlog) SetMenusIdle(); // Dialog has closed
  117.                     }
  118.                     break;
  119.                 case kQuitMenuItem:
  120.                     gQuitFlag = true;
  121.                     break;
  122.             }
  123.             break;
  124.  
  125.     }
  126.     HiliteMenu(0);      // unhilight menu title
  127. }
  128.  
  129. // Dim menus while movable modal dialog is displayed
  130. void SetMenusBusy()
  131. {
  132. #ifdef MODAL
  133.     DisableItem(gAppleMenuHandle, 0);
  134.     DisableItem(gFileMenuHandle, 0);
  135. #else
  136.     DisableItem(gFileMenuHandle, kPrefsItem);
  137. #endif
  138.     DrawMenuBar();
  139. }
  140.  
  141. // Display all menu options
  142. void SetMenusIdle()
  143. {
  144. #ifdef MODAL
  145.     EnableItem(gAppleMenuHandle, 0);
  146.     EnableItem(gFileMenuHandle, 0);
  147. #else
  148.     EnableItem(gFileMenuHandle, kPrefsItem);
  149. #endif
  150.     DrawMenuBar();
  151. }
  152.  
  153. /***************************************************
  154.  *  finally, the main program and event loop
  155.  ***************************************************/
  156.  
  157. main()
  158. {
  159.     EventRecord    mainEventRec;
  160.     Boolean        eventFlag;
  161.     WindowPtr    whichWindow;
  162.     short        result;
  163.     
  164.     // initialize the toolbox
  165.     InitGraf((Ptr) &qd.thePort);
  166.     InitFonts();
  167.     InitWindows();
  168.     InitMenus();
  169.     TEInit();
  170.     InitDialogs(0);
  171.     InitCursor();
  172.  
  173.     FlushEvents(everyEvent,0);
  174.     MaxApplZone();
  175.  
  176.     // initialize application global
  177.     gQuitFlag = false;
  178.     
  179.     // make the menus
  180.     CreateMenus();
  181.     
  182.     // initialize universal procedure pointers
  183. #ifdef CUSTOM_PROCS
  184.     myClickAction = NewClickActionProc(MyClickAction);
  185.     myEditAction = NewEditActionProc(MyEditAction);
  186. #endif
  187.  
  188.     // main event loop
  189.     while (!gQuitFlag) {
  190.  
  191.         eventFlag = WaitNextEvent(everyEvent, &mainEventRec, 60*60*60, nil);
  192.         
  193.         // Inline version of AdjustMenus
  194.         AbleDisItem(kFileMenuID, kDisplayItem, thePrefs != NULL);
  195.         AbleDisItem(kFileMenuID, kAllocateHandle, thePrefs == NULL);
  196.  
  197.         // Let multi-pane dialog have a go at it first
  198.         if (DoMPDialogEvent(&prefDlog, &mainEventRec, &result)) {
  199.             if (!prefDlog) SetMenusIdle(); // Dialog has closed
  200.             continue;
  201.         }
  202.  
  203.         // Handle our About dialog box        
  204.         if (IsDialogEvent(&mainEventRec)) {
  205.             DialogPtr theDialog;
  206.             short itemHit;
  207.             
  208.             if (DialogSelect(&mainEventRec, &theDialog, &itemHit))
  209.                 continue;
  210.         }
  211.         
  212.         // The multi-pane dialog code didn't handle it, we have to.
  213.         switch(mainEventRec.what) {
  214.         
  215.             case mouseDown:
  216.                 switch (FindWindow(mainEventRec.where, &whichWindow)) {
  217.     
  218.                     case inSysWindow:  // desk accessory window
  219.                         SystemClick(&mainEventRec, whichWindow);
  220.                         break;
  221.                         
  222.                     case inMenuBar:
  223.                         AbleDisItem(kFileMenuID, kCloseItem, FrontWindow() != NULL);
  224.                         DoMenuCommand(MenuSelect(mainEventRec.where));
  225.                         break;
  226.                         
  227.                     case inContent:
  228. #ifdef MODAL
  229.                         if (!prefDlog) {
  230. #endif
  231.                         if (whichWindow != FrontWindow()) {
  232.                             SelectWindow(whichWindow);
  233.                         }
  234. #ifdef MODAL
  235.                         } else SysBeep(2);
  236. #endif
  237.                         break;
  238.                     
  239.                     case inGoAway:
  240. #ifdef MODAL
  241.                         if (!prefDlog) {
  242. #endif
  243.                         if (gAbout == whichWindow &&
  244.                                     TrackGoAway(whichWindow, mainEventRec.where)) {
  245.                             DisposeDialog(gAbout);
  246.                             gAbout = NULL;
  247.                         }
  248. #ifdef MODAL
  249.                         } else SysBeep(2);
  250. #endif
  251.                     
  252.                     case inDrag:
  253. #ifdef MODAL
  254.                         if (!prefDlog) {
  255. #endif
  256.                         DragWindow(whichWindow, mainEventRec.where, &qd.screenBits.bounds);
  257. #ifdef MODAL
  258.                         } else SysBeep(2);
  259. #endif
  260.                         break;
  261.  
  262.                     default:
  263.                         // This application really doesn't do very much.
  264.                         break;
  265.                 }
  266.                 break;
  267.  
  268.             case keyDown:
  269.             case autoKey:
  270.                 if (mainEventRec.modifiers & cmdKey) {
  271.                     long mk = MenuKey(mainEventRec.message & charCodeMask);
  272.                     if (mk) {
  273.                         DoMenuCommand(mk);
  274.                         break;
  275.                     }
  276.                 }
  277.                 break;
  278.                 
  279.             // a real app should handle all of these events.
  280.             // Since the code is in Inside Mac:Mac Toolbox Essentials,
  281.             // you really have no excuse for not supporting all the
  282.             // standard low-level events
  283.             // however, this is just a sample program, so I have an excuse
  284.             
  285.             case updateEvt:
  286.             case activateEvt:
  287.             case osEvt:
  288.             case diskEvt:
  289.             case mouseUp:
  290.                 break;
  291.         }
  292.     }
  293.     
  294.     // Good night
  295. }
  296.